package com.lollipop.programming.sort;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * @Author Lollipop
 * @Date 2025/4/7 10:12
 * @Description 冒泡排序
 */
@Slf4j
public class BubbleSort {
    public static void main(String[] args) {
        int[] array = {10, 7, 8, 9, 1, 5, 16, 3};
        int n = array.length;
        // 输出未排序数组
        log.info("未排序数组:{}", Arrays.toString(array));

        int[] sortedArray = bubbleSort(array, n);
        // 输出已排序数组
        log.info("已排序数组:{}", Arrays.toString(sortedArray));
    }

    private static int[] bubbleSort(int[] array, int n) {
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
        return array;
    }
}
